﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_HMS.Classes;

namespace Delonix_Regia_HMS
{
    public partial class Payment : Form
    {
        //
        public Payment(int checkInId, string nric, DateTime actualCheckOutDate, string actualCheckOutTime, int totalDaysStayed, double additionalCost, double totalRoomPrice, double grandTotal)
        {
            InitializeComponent();

            ListViewItem checkOutInformation = new ListViewItem(checkInId.ToString());
            checkOutInformation.SubItems.Add(nric);
            checkOutInformation.SubItems.Add(actualCheckOutDate.ToString());
            checkOutInformation.SubItems.Add(actualCheckOutTime);
            checkOutInformation.SubItems.Add(totalDaysStayed.ToString());
            checkOutInformation.SubItems.Add(additionalCost.ToString());
            checkOutInformation.SubItems.Add(totalRoomPrice.ToString());
            checkOutInformation.SubItems.Add(grandTotal.ToString());
            listViewPaymentCheckOutInformation.Items.Add(checkOutInformation);



            /*When the user is selecting the year of credit card expiry date i give a 20 year window 
             * since credit card usually expire within 10-20 years*/
            cbxPaymentCreditCardExpiryDateYear.Items.Add(DateTime.Now.Year);
            for (int i = 1; i < 21; i++)
            {
                DateTime year = DateTime.Now.AddYears(i);
                cbxPaymentCreditCardExpiryDateYear.Items.Add(year.Year.ToString());
            }
            if (listViewPaymentCheckOutInformation.Items.Count > 0)
            {
                DBManager.DisplayAllItemsConsumeByCheckInId(listViewPaymentItemConsumeInformation, Convert.ToInt32(listViewPaymentCheckOutInformation.Items[0].Text));
            }
            else
            {
                MessageBox.Show("This customer have not consumed any items in the mini bar!");
            }
        }

        int checkInId = 0;

        //When the user is entering the credit card number, the textbox will auto move to the next textbox after the 4 numbers have been filled up
        public void autoExit(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
            if (tbxPaymentCardNumber1.TextLength == 3)
            {
                tbxPaymentCardNumber2.Focus();
            }
            else if (tbxPaymentCardNumber2.TextLength == 3)
            {
                tbxPaymentCardNumber3.Focus();
            }
            else if (tbxPaymentCardNumber3.TextLength == 3)
            {
                tbxPaymentCardNumber4.Focus();
            }
        }

        private void cbxPaymentPaymentMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxPaymentPaymentMode.Text.Equals("Credit Card"))
            {
                label4.Show();
                label5.Show();
                label26.Show();
                label27.Show();
                label28.Show();
                label29.Show();
                label30.Show();
                tbxPaymentCardNumber1.Show();
                tbxPaymentCardNumber2.Show();
                tbxPaymentCardNumber3.Show();
                tbxPaymentCardNumber4.Show();
                cbxPaymentCreditCardExpiryDateYear.Show();
                cbxPaymentCreditCardExpiryDateMonth.Show();
            }
            else//Cash Payment
            {
                label4.Hide();
                label5.Hide();
                label26.Hide();
                label27.Hide();
                label28.Hide();
                label29.Hide();
                label30.Hide();
                tbxPaymentCardNumber1.Hide();
                tbxPaymentCardNumber2.Hide();
                tbxPaymentCardNumber3.Hide();
                tbxPaymentCardNumber4.Hide();
                cbxPaymentCreditCardExpiryDateYear.Hide();
                cbxPaymentCreditCardExpiryDateMonth.Hide();
            }
        }

        private void btnCustomerCheckOutConfirmCheckOut_Click(object sender, EventArgs e)
        {
            bool successfulCreate = false;
            if (string.IsNullOrEmpty(cbxPaymentPaymentMode.Text))
            {
                MessageBox.Show("Please select a Payment Mode!");
            }
            else if (cbxPaymentPaymentMode.Text.Equals("Credit Card"))//credit card payment
            {
                if (tbxPaymentCardNumber1.Text.Length < 4 || tbxPaymentCardNumber2.Text.Length < 4 || tbxPaymentCardNumber3.Text.Length < 4 || tbxPaymentCardNumber4.Text.Length < 4)
                {
                    MessageBox.Show("Please enter the 16 credit card number!");
                }
                else if (string.IsNullOrEmpty(cbxPaymentCreditCardExpiryDateYear.Text) || string.IsNullOrEmpty(cbxPaymentCreditCardExpiryDateMonth.Text))
                {
                    MessageBox.Show("Please select the credit expiry year and month!");
                }
                else//Everything filled up
                {
                    string creditCardNumber = tbxPaymentCardNumber1.Text + "-" + tbxPaymentCardNumber2.Text + "-" + tbxPaymentCardNumber3.Text + "-" + tbxPaymentCardNumber4.Text;
                    string creditCardExpiryDate = cbxPaymentCreditCardExpiryDateYear.Text + "/" + cbxPaymentCreditCardExpiryDateMonth.Text;
                    CheckOut cO = new CheckOut();
                    cO.CheckInID = Convert.ToInt32(listViewPaymentCheckOutInformation.Items[0].Text);
                    cO.Nric = listViewPaymentCheckOutInformation.Items[0].SubItems[1].Text;
                    cO.ActualCheckOutDate = Convert.ToDateTime(listViewPaymentCheckOutInformation.Items[0].SubItems[2].Text);
                    cO.ActualCheckOutTime = listViewPaymentCheckOutInformation.Items[0].SubItems[3].Text;
                    cO.TotalDaysStay = Convert.ToInt32(listViewPaymentCheckOutInformation.Items[0].SubItems[4].Text);
                    cO.AdditionalCost = Convert.ToDouble(listViewPaymentCheckOutInformation.Items[0].SubItems[5].Text);
                    cO.TotalRoomPrice = Convert.ToDouble(listViewPaymentCheckOutInformation.Items[0].SubItems[6].Text);
                    cO.GrandTotal = Convert.ToDouble(listViewPaymentCheckOutInformation.Items[0].SubItems[7].Text);
                    cO.PaymentMode = cbxPaymentPaymentMode.Text;
                    cO.CreditCardNumber = creditCardNumber;
                    cO.CreditCardExpiryDate = creditCardExpiryDate;
                    DBManager.CustomerCheckOut(cO);
                    successfulCreate = true;

                }
            }
            else//cash payment
            {
                CheckOut cO = new CheckOut();
                checkInId = Convert.ToInt32(listViewPaymentCheckOutInformation.Items[0].Text);
                cO.CheckInID = Convert.ToInt32(listViewPaymentCheckOutInformation.Items[0].Text);
                cO.Nric = listViewPaymentCheckOutInformation.Items[0].SubItems[1].Text;
                cO.ActualCheckOutDate = Convert.ToDateTime(listViewPaymentCheckOutInformation.Items[0].SubItems[2].Text);
                cO.ActualCheckOutTime = listViewPaymentCheckOutInformation.Items[0].SubItems[3].Text;
                cO.TotalDaysStay = Convert.ToInt32(listViewPaymentCheckOutInformation.Items[0].SubItems[4].Text);
                cO.AdditionalCost = Convert.ToDouble(listViewPaymentCheckOutInformation.Items[0].SubItems[5].Text);
                cO.TotalRoomPrice = Convert.ToDouble(listViewPaymentCheckOutInformation.Items[0].SubItems[6].Text);
                cO.GrandTotal = Convert.ToDouble(listViewPaymentCheckOutInformation.Items[0].SubItems[7].Text);
                cO.PaymentMode = cbxPaymentPaymentMode.Text;
                cO.CreditCardNumber = null;
                cO.CreditCardExpiryDate = null;
                DBManager.CustomerCheckOut(cO);
                successfulCreate = true;
            }
            if (successfulCreate == true)
            {
                MessageBox.Show("Customer Check Out Successful!");
                DBManager.UpdateCustomerCheckOutRoomStatus(checkInId);
                DBManager.UpdateBookingStatusCheckOut(checkInId);
            }
            else
            {
                MessageBox.Show("Customer Check Out NOT Successful!");
            }
        }

    }
}
